
#include <TFConfigurator.h>
#include <gtest/gtest.h>
#include <map>
#include <string>
using namespace std;

const char *testdb="tf.dat";
const char *table="config";

namespace {
    class TFConfiguratorTest: public ::testing::Test {
    public:
        TFConfiguratorTest(){
        //            :config2("test2.cfg"){
        }
        virtual ~TFConfiguratorTest(){
        }
        virtual void SetUp(){
        }
        virtual void TearDown(){
        }
    private:
        //      TFConfigurator config2;
    };
}

//Constructor test;
TEST_F(TFConfiguratorTest,ConstructorTest){
    TFConfigurator *pconfig=new TFConfigurator(testdb,table,false);
    EXPECT_NE((int)pconfig,0);
    delete pconfig;
    pconfig=NULL;

    pconfig=new TFConfigurator(testdb,table);
    EXPECT_NE((int)pconfig,0);
    delete pconfig;
}

TEST_F(TFConfiguratorTest, ReadOnlyConfigTest){
    TFConfigurator config(testdb,table);
    bool readonly=config.ReadOnly();
    EXPECT_EQ(readonly,false);
    string k("KeyAbcedfg"),v("Myvafdsawe");
    bool setRet=config.SetConfigValue(k,v);
    EXPECT_EQ(setRet,true);

    TFConfigurator config_ro(testdb,table,true);
    EXPECT_EQ(config_ro.ReadOnly(),true);
    setRet=config_ro.SetConfigValue(k,v);
    EXPECT_EQ(setRet,false);
}

//Set and get test;
TEST_F(TFConfiguratorTest , ConfigSetAndGetTest) {
    string cfgfile(testdb,table);
    TFConfigurator config(cfgfile.c_str(),false);
    string k("key");
    string v("value");
    string re;
    //config.ShowAll();

    //std::string get&set test
    bool setRet=config.SetConfigValue(k,v);
    EXPECT_EQ(setRet,true);
    bool modified=config.Modified();
    EXPECT_EQ(modified,true);
    bool readonly=config.ReadOnly();
    EXPECT_EQ(readonly,false);
    re=config.GetConfigValue<string>(k);
    EXPECT_EQ(re,v);
    //config.ShowAll();

    //int get&set test
    int ivalue=1024;
    setRet=config.SetConfigValue(k,ivalue);
    EXPECT_EQ(setRet,true);
    int igetvalue=config.GetConfigValue<int>(k);
    re.clear();
    re=config.GetConfigValue<string>(k);
    EXPECT_EQ(re,"1024");
    EXPECT_EQ(ivalue,igetvalue);
    //config.ShowAll();
    
    //double get&set test
    double fvalue=234.342512334;
    setRet=config.SetConfigValue(k,fvalue);
    EXPECT_EQ(setRet,true);
    double fgetvalue=config.GetConfigValue<double>(k);
    EXPECT_LT( abs(fgetvalue-fvalue),0.001 );
    //config.ShowAll();
}

int main(int argc,char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
